package com.crime.crimemonitorclient;

import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;
import java.util.Calendar;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.DatePicker;
import android.widget.Toast;

import com.androidplot.xy.BoundaryMode;
import com.androidplot.xy.XYPlot;

public class DisplayCrimeDetailActivity extends Activity
{
	private int mTsk;
	private int mStartYear;
	private int mStartMonth;
	private int mEndYear;
	private int mEndMonth;
	private static final int SELECT_CRIME_INTERVAL_DATE_DIALOG = 10;
	
	private PlotFragment mPlotFragment;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_display_crime_detail);
		
		Bundle contextData = this.getIntent().getExtras();
		
		if(contextData != null)
		{
			this.mTsk = contextData.getInt("tsk");
			this.mStartYear = contextData.getInt("startYear");
			this.mStartMonth = contextData.getInt("startMonth");
			this.mEndYear = contextData.getInt("endYear");
			this.mEndMonth = contextData.getInt("endMonth");
		}
		
		this.mPlotFragment = new PlotFragment(this, (XYPlot)this.findViewById(R.id.crime_detail_timeseries_graph));
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		// refresh detail data
		//this.refreshData();
	}
	
	@Override
	public void onBackPressed() {
		if(!this.mPlotFragment.goBack())
			super.onBackPressed();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.display_crime_detail, menu);
		return true;
	}
	
	private void refreshData()
	{
		// clear time series data
		//ListView timeSeriesListView = (ListView) this.findViewById(R.id.crime_detail_timeseries_list);
		//((CrimeTimeSeriesListAdapter)timeSeriesListView.getAdapter()).clear();
		
		// call async task
//		new GetCrimeDetailAsyncTask(
//				"http://beny.kolej.mff.cuni.cz:8080/crimeservice/rest/service/detail",
//				this.mTsk,
//				this.mStartYear, this.mStartMonth,
//				this.mEndYear, this.mEndMonth,
//				this).execute();
	}
	
	@SuppressLint("NewApi")
	@Override
	@Deprecated
	protected Dialog onCreateDialog(int id, Bundle args) {
		switch(id)
		{
			case SELECT_CRIME_INTERVAL_DATE_DIALOG:
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				LayoutInflater inflater = this.getLayoutInflater();
				final View dialogView = inflater.inflate(R.layout.select_crime_interval_dialog, null);
				
				DatePicker startDatePicker = (DatePicker) dialogView.findViewById(R.id.select_crime_interval_startDate);
				startDatePicker.init(this.mStartYear, this.mStartMonth, 1, null);
				this.setUpDatePickerToDisableDays(startDatePicker);
				
				DatePicker endDatePicker = (DatePicker) dialogView.findViewById(R.id.select_crime_interval_endDate);
				endDatePicker.init(this.mEndYear, this.mEndMonth, 1, null);
				this.setUpDatePickerToDisableDays(endDatePicker);
				
				
				builder
					.setTitle(R.id.display_crime_detail_set_interval)
					.setView(dialogView)
					.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
					
						@Override
						public void onClick(DialogInterface dialog, int which) {
							DatePicker startDatePicker = (DatePicker) dialogView.findViewById(R.id.select_crime_interval_startDate);
							DatePicker endDatePicker = (DatePicker) dialogView.findViewById(R.id.select_crime_interval_endDate);
							
							
//							DisplayCrimeDetailActivity.this.onCrimeIntervalSelected(
//									startDatePicker.getYear(), startDatePicker.getMonth(),
//									endDatePicker.getYear(), endDatePicker.getMonth());
						}
					})
					.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
				
				return builder.create();
				
			default:
				return super.onCreateDialog(id, args);
		}
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId())
		{
			case R.id.display_crime_detail_set_interval:
				this.showDialog(SELECT_CRIME_INTERVAL_DATE_DIALOG);
				return true;
			
			default:
				return super.onOptionsItemSelected(item);
		}
	}
	
	private void setUpDatePickerToDisableDays(DatePicker picker)
	{
		try
		{
			Field f[] = picker.getClass().getDeclaredFields();
			for (Field field : f) {
			if (field.getName().equals("mDayPicker")) {
			field.setAccessible(true);
			Object yearPicker = new Object();
			yearPicker = field.get(picker);
			((View) yearPicker).setVisibility(View.GONE);
			}
			}
			} catch (SecurityException e) {
			Log.d("ERROR", e.getMessage());
			} 
			catch (IllegalArgumentException e) {
			Log.d("ERROR", e.getMessage());
			} catch (IllegalAccessException e) {
			Log.d("ERROR", e.getMessage());
		}
	}
}


















